﻿using Microsoft.Extensions.Logging;
using NuGet.Protocol.Core.Types;
using QuickGet.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickGet.Core.Services
{
    public class NugetResourceResolver : INugetResourceResolver
    {
        protected virtual ILogger _logger { get; set; }
        protected virtual INugetSourceRepositoryResolver _sourceRepoResolver { get; set; }
    
    
        public NugetResourceResolver(
            ILogger<NugetResourceResolver> logger,
            INugetSourceRepositoryResolver sourceRepoResolver)
        {
            _logger = logger;
            _sourceRepoResolver = sourceRepoResolver;
        }

        public async Task<TResource?> GetResourceAsync<TResource>(string packageSourceName, CancellationToken cancellationToken) where TResource : class, INuGetResource
        {
            var repo = await _sourceRepoResolver.GetRepository(packageSourceName, cancellationToken);

            if(repo is null)
            {
                _logger.LogInformation("Package source '{PackageSourceName}' resouce '{ResourceName}' not found", packageSourceName, typeof(TResource).Name);
                return default;
            }

            return await repo.GetResourceAsync<TResource>(cancellationToken);
        }
    }
}
